package com.ybkj.chat.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ybkj.chat.R;


/**
 * 分享弹窗
 *
 * @author wyq
 * @date 2019/2/25
 */

public class PpwShareChannel extends PopupWindow {

    private LayoutInflater mInflater;
    private View mView;

    private LinearLayout mLlWechatFriend, mLlQqZone, mLlSina, mLlWechatCicle, mLlQQ;
    private TextView mTvCancel;

    private OnShareButtonClickListener mOnShareButtonClickListener;

    private Context mContext;
    private Activity mActivity;

    public PpwShareChannel(Context context) {
        super(context);
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        init();
        setPopwindow();
    }

    private void init() {
        mView = mInflater.inflate(R.layout.pop_share_channel, null);

        mLlWechatFriend = (LinearLayout) mView.findViewById(R.id.ll_wechat);
        mLlQqZone = (LinearLayout) mView.findViewById(R.id.ll_qq_zone);
        mLlSina = (LinearLayout) mView.findViewById(R.id.ll_sina);
        mLlWechatCicle = (LinearLayout) mView.findViewById(R.id.ll_wechat_circle);
        mTvCancel = (TextView) mView.findViewById(R.id.tv_cancel);
        mLlQQ = (LinearLayout) mView.findViewById(R.id.ll_qq);

        mLlWechatFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnShareButtonClickListener != null) {
                    mOnShareButtonClickListener.onWechatFriendShareClick();
                }
                dismiss();
            }
        });

        mLlQqZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnShareButtonClickListener != null) {
                    mOnShareButtonClickListener.onQQZoneShareClick();
                }
                dismiss();
            }
        });

        mLlSina.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnShareButtonClickListener != null) {
                    mOnShareButtonClickListener.onSinaShareClick();
                }
                dismiss();
            }
        });

        mLlWechatCicle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnShareButtonClickListener != null) {
                    mOnShareButtonClickListener.onWechatCircleShare();
                }
                dismiss();
            }
        });

        mLlQQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnShareButtonClickListener != null) {
                    mOnShareButtonClickListener.onQQShareClick();
                }
                dismiss();
            }
        });

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void setPopwindow() {
        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        //this.setAnimationStyle(R.style.ppw_select_picture);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setFocusable(true);
        this.setOutsideTouchable(true);


        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mActivity != null) {
                    setBackgroundAlpha(mActivity, 1f);
                }
            }
        });
    }

    /**
     * 设置页面的透明度
     *
     * @param bgAlpha 1表示不透明
     */
    public void setBackgroundAlpha(Activity activity, float bgAlpha) {
        this.mActivity = activity;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    public void setOnShareButtonCLickListener(OnShareButtonClickListener onShareButtonClickListener) {
        this.mOnShareButtonClickListener = onShareButtonClickListener;
    }

    public interface OnShareButtonClickListener {

        //微信好友分享
        void onWechatFriendShareClick();

        //QQ空间分享
        void onQQZoneShareClick();

        //新浪微博分享
        void onSinaShareClick();

        //微信朋友圈分享
        void onWechatCircleShare();

        //QQ分享
        void onQQShareClick();
    }

}
